package net.aiion.weave.impl.ui;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import net.aiion.weave.spi.cards.annotations.Access;
import net.aiion.weave.spi.cards.annotations.Property;
import net.aiion.weave.spi.ui.Component;
import net.aiion.weave.spi.ui.Container;

public class AbstractContainer extends AbstractComponent implements Container
{
    @Property(Access.READ_ONLY)
    private List<Component> children;

    public AbstractContainer() {
        children = new ArrayList<>();
    }

    @Override
    public void addComponent(final Component aComponent) {
        if (aComponent.getParent() != this) {
            children.add(aComponent);
            aComponent.setParent(this);
        }
    }

    @Override
    public Component getComponent(final int aIndex) {
        return children.get(aIndex);
    }

    @Override
    public Collection<Component> getComponents() {
        return Collections.unmodifiableCollection(children);
    }

    @Override
    public void removeComponent(final Component aComponent) {
        children.remove(aComponent);
    }
}
